package ligne2vie.topos.client.panels;

import ligne2vie.topos.client.gwt.proxy.MyAnchor;
import ligne2vie.topos.client.maps.MyMapWidget;
import ligne2vie.topos.client.maps.events.MapEvent;
import ligne2vie.topos.client.maps.events.MapObserver;
import ligne2vie.topos.client.maps.events.SummitClickEvent;
import ligne2vie.topos.client.process.SummitAddProcess;
import ligne2vie.topos.client.process.SummitDeleteProcess;
import ligne2vie.topos.client.process.SummitEditProcess;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * The class {@link SummitEditPanel} proposes users all actions about summit : add, edit, delete, ... This panel is
 * designed to be integrated into {@link ControlPanel}.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public class SummitEditPanel extends VerticalPanel implements MapObserver {

	private MyAnchor addSummitButton;
	private MyAnchor editSummitButton;
	private MyAnchor deleteSummitButton;

	private HandlerRegistration editButtonHandler;
	private HandlerRegistration deleteButtonHandler;

	/**
	 * Default constructor.
	 * 
	 * @param actionProcessPanel
	 *            The panel where actions have their form displayed.
	 */
	public SummitEditPanel() {

		// --- instantiating addSummitButton

		addSummitButton = new MyAnchor("Ajouter un sommet");
		addSummitButton.setEnabled(true);
		addSummitButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				new SummitAddProcess();
			}
		});

		// --- instantiating editSummitButton

		editSummitButton = new MyAnchor("Modifier un sommet");
		editSummitButton.setEnabled(false);

		// --- instantiating deleteSummitButton

		deleteSummitButton = new MyAnchor("Supprimer un sommet");
		deleteSummitButton.setEnabled(false);

		// --- composing panel

		this.add(addSummitButton);
		this.add(editSummitButton);
		this.add(deleteSummitButton);

		// --- listening to map event

		MyMapWidget.getInstance().addObserver(this);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void notifyMapEvent(MapEvent event) {

		if (event instanceof SummitClickEvent) {

			final SummitClickEvent clickEvent = (SummitClickEvent) event;

			this.editSummitButton.setEnabled(true);
			this.editButtonHandler = this.editSummitButton.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					new SummitEditProcess(clickEvent.getSummitId());
				}
			});

			this.deleteSummitButton.setEnabled(true);
			this.deleteButtonHandler = this.deleteSummitButton.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					new SummitDeleteProcess(clickEvent.getSummitId());
				}
			});

		} else {

			this.editSummitButton.setEnabled(false);
			if (this.editButtonHandler != null) {
				this.editButtonHandler.removeHandler();
				this.editButtonHandler = null;
			}

			this.deleteSummitButton.setEnabled(false);
			if (this.deleteButtonHandler != null) {
				this.deleteButtonHandler.removeHandler();
				this.deleteButtonHandler = null;
			}
		}

	}
}
